package com.eternalrights.service.impl;

import com.eternalrights.mapper.EmpExprMapper;
import com.eternalrights.mapper.EmpMapper;
import com.eternalrights.pojo.*;
import com.eternalrights.service.EmpLogService;
import com.eternalrights.service.EmpService;
import com.eternalrights.utils.JwtUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private EmpExprMapper empExprMapper;

    @Autowired
    private EmpLogService empLogService;

//    @Override
//    public PageResult<Emp> page(Integer page,Integer pageSize) {
//        long total = empMapper.count();
//        Integer start = (page - 1) * pageSize;
//        List<Emp> list = empMapper.list(start,pageSize);
//        return new PageResult<Emp>(total,list);
//    }
    @Override
    public PageResult<Emp> page(EmpQueryResult empQueryResult) {
        PageHelper.startPage(empQueryResult.getPage(),empQueryResult.getPageSize());
        List<Emp> list = empMapper.list(empQueryResult);
        Page p = (Page) list;
        return new PageResult<Emp>(p.getTotal(),p.getResult());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Emp emp){
        try {
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.insert(emp);

            List<EmpExpr> exprList = emp.getExprList();
            if (!CollectionUtils.isEmpty(exprList)){
                exprList.forEach(empExpr->empExpr.setEmpId(emp.getId()));
                empExprMapper.insertBatch(exprList);
            }
        } finally {
            EmpLog emplog = new EmpLog(null,LocalDateTime.now(),"添加员工" + emp);
            empLogService.insertLog(emplog);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Integer> ids){
//        1.批量删除员工信息
        empMapper.deleteByIds(ids);

//        2.批量删除员工工作经历信息
        empExprMapper.deleteByExprIds(ids);
    }

    @Override
    public Emp getInfo(Integer id){
        return empMapper.getById(id);
    }

    @Override
    public void update(Emp emp){
        emp.setUpdateTime(LocalDateTime.now());
//        1.修改员工数据
        empMapper.updateById(emp);

//        2.删除原员工表的员工工作经历数据
        empExprMapper.deleteByExprIds(Arrays.asList(emp.getId()));

//        3.批量插入新的员工工作经历数据
        List<EmpExpr> exprList = emp.getExprList();
        if ( !CollectionUtils.isEmpty(exprList)){
            exprList.forEach(empExpr->empExpr.setEmpId(emp.getId()));
            empExprMapper.insertBatch(exprList);
        }
    }

    @Override
    public List<Emp> findEmp() {
        return empMapper.findEmp();
    }

    @Override
    public LoginInfo login(Emp emp) {
        Emp empTemp = empMapper.selectByUsernameAndPassword(emp);

        if ( empTemp != null){
            Map<String,Object> map = new HashMap<>();
            map.put("id",empTemp.getId());
            map.put("username",empTemp.getUsername());
            String token = JwtUtils.generateJwt(map);

            return new LoginInfo(empTemp.getId(),empTemp.getUsername(),empTemp.getPassword(),token);
        }

        return null;
    }
}
